{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 20 迭代器与生成器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【选择】下列关于Python“可迭代对象”和“迭代器”说法错误的是：（ ）\n",
    "\n",
    "A.能够接收“迭代器”的函数都可以接收“可迭代对象”  \n",
    "B.迭代器可以被内置函数next()调用，不断返回下一个值的对象  \n",
    "C.内置函数iter()可以将“可迭代对象”转换成“迭代器”  \n",
    "D.可迭代对象不一定是迭代器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】下列Python语句的运行结果是：（ ）  \n",
    "`\n",
    "lst = [\"a\",\"b\",\"c\"]\n",
    "myIterator = iter(lst)\n",
    "print(next(myIterator))\n",
    "next(myIterator)\n",
    "print(next(myIterator))`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "a  \n",
    "c  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【判断】迭代器可以作用于循环语句while，可以直接遍历下一个值对象。（ ）\n",
    "\n",
    "A.√  \n",
    "B.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【选择】下列关于Python的描述正确的是：（ ）\n",
    "\n",
    "A.调用range()函数返回的是生成器  \n",
    "B.生成器中一般采用的是return语句来返回结果  \n",
    "C.调用生成器的时候不会立即返回结果，因为生成器遵循“惰性计算”  \n",
    "D.生成器中包含迭代器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【填空】下列Python语句的运行结果是：（ ）  \n",
    "`\n",
    "def myGen():\n",
    "    for i in range(5):\n",
    "        yield  i**2\n",
    "for x in myGen():\n",
    "    print(x,end=\" \")`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：0 1 4 9 16 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.【填空】下列Python语句的运行结果是：（ ）  \n",
    "`\n",
    "def myGen():\n",
    "    a = [26,90,73,47,18]\n",
    "    for i in range (len(a)-1):\n",
    "        if(a[i] > a[i+1]):\n",
    "            a[i],a[i+1] = a[i+1],a[i]\n",
    "        yield  a[i]\n",
    "for x in myGen():\n",
    "    print(x,end=\",\")`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：26,73,47,18,"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
